<?php

/*
 * B端操作功能模块
 * 功能：
 */

namespace Home\Controller;

use Think\B_Controller;

class StoreController extends B_Controller {

    public function __construct() {
        parent::__construct();

    }

    public function index() {
        if ($this->user['is_admin'] == IS_ADMIN) {
            $c = D('Companystoremap', 'Logic');
            $company = $c->getCompanyMap($_SESSION['id']);
            foreach($company as $v){
                $cid[] = $v['store_id'];
            }
            $store = $this->s->getStoresByIds($cid);
        } else {
            $store = $this->s->getStores();
        }

        if (count($store) < 1) {
            $a = D('Area', 'Logic');
            $privinces = $a->getProvinces();
            $this->assign('provinces', $privinces);
            $this->display('create');
        } else {
            $this->assign('is_admin', $this->user['is_admin']);
            $this->assign('store', $store);
            $this->display('store');
        }
    }
    
    public function creates(){
        $a = D('Area', 'Logic');
        $privinces = $a->getProvinces();
        $this->assign('provinces', $privinces);
        $this->assign('store','');
        $this->display('create');
    }

    public function code() {
        layout(false);
        $this->display('code');
    }

    public function img() {
        layout(false);
        $this->display('img');
    }

    public function upload() {
        layout(false);
        import('Think.upload');
        $u = new \Think\Upload();
        $path = $u->upload($_FILES);
        $array = array_keys($path);
        if ($array[0] == 'code') {
            $this->assign('path', '/public/upload/' . $path['code']['savepath'] . $path['code']['savename']);
            $this->display('code');
        } else {
            $this->assign('path', '/public/upload/' . $path['img']['savepath'] . $path['img']['savename']);
            $this->display('img');
        }
    }

    public function create() {

        $data['user_id'] = (int) $_SESSION['id'];
        $data['id'] = (int) $_POST['id'];
        $data['name'] = trim($_POST['name']);
        $data['address'] = trim($_POST['address']);
        $data['person_charge'] = trim($_POST['person_charge']);
        $data['tel'] = trim($_POST['tel']);
        $data['province'] = (int) $_POST['province'];
        $data['city'] = (int) $_POST['city'];
        $data['area'] = (int) $_POST['area'];
        $data['latlng'] = str_replace(' ', '', $_POST['latlng']);
        $data['logo'] = $_POST['banner_logo'];
        $data['code'] = $_POST['code_img'];
        if (strlen($data['name']) < 1 || strlen($data['address']) < 1 || strlen($data['person_charge']) < 1 || strlen($data['tel']) < 1 || $data['province'] < 1 || $data['city'] < 1 || $data['area'] < 1 || strlen($data['logo']) < 1 || strlen($data['code']) < 1) {
            $this->_msg('_SYSTEM_ERROR_');
        }
        if ($data['id'] > 0) {
            $cn = D('Companystoremap','Logic');
            $com = $cn->getCompanyMap($data['user_id'],true,$data['id']);
            $store = $this->s->getStore($data['id']);
            if ($store['user_id'] != $data['user_id'] && $data['user_id']!=$com[0]['admin_id']) {
                $this->_msg('_HAVE_NO_LEGAL_POWER_');
            }
            $uid = (int)$_POST['principal'];
            if($data['user_id']==$com[0]['admin_id'] && $uid>0){
                $data['barber_id'] = $uid;
            }
            $re = $this->s->updateStore($data);
            $re = 1;//更新不报错
        } else {
            $re = $this->s->insertStore($data);
        }
        if ($re < 1) {
            $this->_msg('_SYSTEM_ERROR_');
        }
        redirect('/home/store');
    }

    public function edit() {
        $id = (int) $_GET['id'];
        $store = $this->s->getStore($id);
        $cn = D('Companystoremap','Logic');
        $com = $cn->getCompanyMap($_SESSION['id'],true,$id);
        if ($store['user_id'] != $_SESSION['id'] && $_SESSION['id']!=$com[0]['admin_id']) {
            $this->_msg('_HAVE_NO_LEGAL_POWER_');
        }
        $a = D('Area', 'Logic');
        $privinces = $a->getProvinces();
        $cities = $a->getCities($store['province']);
        $areas = $a->getAreas($store['city']);
        $this->assign('provinces', $privinces);
        $this->assign('cities', $cities);
        $this->assign('areas', $areas);
        $this->assign('store', $store);
        if($_SESSION['id']==$com[0]['admin_id']){ //是否超管
            $this->assign('edit',1);
        }
        $b = D('Barber','Logic');
        $barbers = $b->getBarberByStoreId($id);
        $p = D('Passport','Logic');
        $user = $p->getUser($_SESSION['id']);
        foreach($barbers as $k=>$v){
            if($v['mobile']==$user['account']){
                unset($barbers[$k]);
            }
        }
        $this->assign('barbers',$barbers);
        $this->display('create');
    }

    public function getCities() {
        $id = (int) $_GET['id'];
        if ($id < 1) {
            return false;
        }
        $c = D('area', 'Logic');
        echo json_encode($c->getCities($id));
    }

    public function getAreas() {
        $id = (int) $_GET['id'];
        if ($id < 1) {
            return false;
        }
        $c = D('area', 'Logic');
        echo json_encode($c->getAreas($id));
    }


    public function configure(){
        $admin_id = $_SESSION['id'];
        $com = new \Home\Logic\CompanystoremapLogic();
        $company_number = $com->getByAdminId($admin_id);
        $this->assign('company_number',$company_number);
        $this->display('configure');
    }


}
